package krow.dev.gunpobus.view.fragment;

import java.util.List;

import krow.dev.gunpobus.Constants.Invoke;
import krow.dev.gunpobus.Constants.Value;
import krow.dev.gunpobus.NodeActivity;
import krow.dev.gunpobus.R;
import krow.dev.gunpobus.ScheduleActivity;
import krow.dev.gunpobus.control.FavoriteSelectTask;
import krow.dev.gunpobus.model.FavoriteItem;
import krow.dev.gunpobus.view.adapter.FavoriteListAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class FavoriteFragment extends BaseFragment {

	private Context mContext;
	
	private FavoriteListAdapter mFavoriteListAdapter;
	private ListView mFavoriteListView;
	
	private FavoriteSelectTask mFavoriteSelectTask;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		mContext = inflater.getContext();
		
		View view = inflater.inflate(R.layout.fragment_favorite, null);

		mFavoriteListAdapter = new FavoriteListAdapter(mContext);
		mFavoriteListView = (ListView) view.findViewById(R.id.listview_favorite);
		mFavoriteListView.addFooterView(inflater.inflate(R.layout.footer_dummy, null));
		mFavoriteListView.setAdapter(mFavoriteListAdapter);
		mFavoriteListView.setOnItemClickListener(mItemClickListener);
		
		return view;
	}

	@Override
	public void onResume() {
		super.onResume();

		requestFavorite();
	}

	@Override
	public void onPause() {
		if(mFavoriteSelectTask != null){
			mFavoriteSelectTask.cancel(true);
		}
		super.onPause();
	}

	private void requestFavorite(){
		
		setLoadBarVisibility(View.VISIBLE);
		
		mFavoriteSelectTask = new FavoriteSelectTask();
		mFavoriteSelectTask.setCompletionListener(mCompletionListener);
		mFavoriteSelectTask.execute();
	}
	
	private FavoriteSelectTask.OnCompletionListener mCompletionListener = new FavoriteSelectTask.OnCompletionListener() {
		
		@Override
		public void onCompletion(List<FavoriteItem> favoriteItemList) {
			setLoadBarVisibility(View.GONE);
			
			mFavoriteListAdapter.setFavoriteItemList(favoriteItemList);
			mFavoriteListAdapter.notifyDataSetChanged();
		}
	};
	
	private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if(view.getTag() == null){
				return ;
			}
			FavoriteItem favoriteItem = (FavoriteItem) view.getTag();
			
			Intent intent = null;
			switch (favoriteItem.getType()) {
			case Value.TYPE_FAVORITE_ROUTE:
				intent = new Intent(getActivity(), NodeActivity.class);
				intent.putExtra(Invoke.KEY_ROUTE_NAME, favoriteItem.getName());
				intent.putExtra(Invoke.KEY_ROUTE_ID, favoriteItem.getId());
				intent.putExtra(Invoke.KEY_ROUTE_TYPE, favoriteItem.getRouteType());
				break;
			case Value.TYPE_FAVORITE_STATION:
				intent = new Intent(getActivity(), ScheduleActivity.class);
				intent.putExtra(Invoke.KEY_STATION_NAME, favoriteItem.getName());
				intent.putExtra(Invoke.KEY_STATION_ID, favoriteItem.getId());
				intent.putExtra(Invoke.KEY_STATION_SERIAL, favoriteItem.getStationSerial());
				break;
			}
			
			if(intent != null){
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.right_in, R.anim.scale_out);
			}
		}
	};
}
